package com.xxgc.mybatis.dao.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

//Mybaits工具类，用来获取sqlSession
public class MyBatisUtils {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            //以下三步通过配置文件来获取sqlSessionFactory
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            /* ctrl + alt + f 提取/抽取 全局变量 */
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e){
            e.printStackTrace();
        }
    }



    public static SqlSession getSqlSession(){
        /* ctrl + alt + v 自动生成返回结果 */
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}